package redBook;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.PriorityQueue;

public class Main {

    public static void main(String[] args) throws Exception{
        BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
        String s = buf.readLine();
        int n = Integer.parseInt(s);
        PriorityQueue<Integer> white = new PriorityQueue<>();
        PriorityQueue<Integer> black = new PriorityQueue<>();
        int whiteCount = 0;
        int blackCount = 0;
        String[] ss;
        for (int i = 0; i < n; i++){
            s = buf.readLine();
            if (s.startsWith("0")){
                ss = s.split(" ");
                white.offer(Integer.parseInt(ss[1]));
                black.offer(Integer.parseInt(ss[2]));
            } else if ("1".equals(s)){
                whiteCount++;
            } else if ("2".equals(s)){
                blackCount++;
            }
        }
        int count = whiteCount - blackCount;
        int res = 0;
        if (count > 0){
            while(!black.isEmpty()){
                res += black.poll();
                count--;
                if (count == 0){
                    System.out.println(res);
                    return;
                }
            }
        } else if (count < 0){
            count = -count;
            while(!white.isEmpty()){
                res += white.poll();
                count--;
                if (count == 0){
                    System.out.println(res);
                    return;
                }
            }
        } else {
            System.out.println(0);
            return;
        }
        System.out.println(-1);
    }


}
